package hong.jun.time;

import hong.jun.task.TaskThreadPoolManger;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 说明:
 * Created on 2023/10/23 11:51
 *
 * @author yanghongjun <1737619387@qq.com>
 */
public class TimeManger {

    /**
     * 用枚举来实现单例
     */
    private enum Singleton {
        INSTANCE;
        final TimeManger processor;

        Singleton() {
            this.processor = new TimeManger();
        }

        TimeManger getProcessor() {
            return processor;
        }
    }

    /**
     * 获取实例对象
     *
     * @return
     */
    public static TimeManger getInstance() {
        return TimeManger.Singleton.INSTANCE.getProcessor();
    }

    private final ScheduledExecutorService executorService;

    private static final AtomicInteger threadCount = new AtomicInteger();
    public TimeManger() {
        ThreadFactory threadFactory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("TimerManager-" + threadCount.incrementAndGet());
                return thread;
            }
        };
        /*executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), r -> {
            Thread thread = new Thread(r);
            thread.setName("TimerManager-" + threadCount.incrementAndGet());
            return thread;
        });*/
        executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), threadFactory);
    }


    public ScheduledExecutorService getExecutorService(){
        return executorService;
    }
}
